package dyy.zc.computergradeexam.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import dyy.zc.computergradeexam.R;
import dyy.zc.computergradeexam.application.MyApplication;
import dyy.zc.computergradeexam.base.BaseActivity;
import dyy.zc.computergradeexam.utils.AppManager;
import dyy.zc.computergradeexam.views.MyAlertDialog;


/**
 * Created by zhouchuan on 2017/2/27.
 */

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private TextView title,right;
    private TextView tvXuanZe, tvTianKong, tvBianCheng;
    private TextView tvXuanZeWrong, tvTianKongWrong, tvBianChengWrong;
    private Banner banner;
    private List<String> list_img = new ArrayList<>();

    @Override
    protected void findViewByIDS() {
        tvXuanZe = (TextView) findViewById(R.id.tv_XuanZe);
        tvTianKong = (TextView) findViewById(R.id.tv_TianKong);
        tvBianCheng = (TextView) findViewById(R.id.tv_BianCheng);
        title = (TextView) findViewById(R.id.top_tv_title);
        right = (TextView) findViewById(R.id.top_tv_right);
        tvXuanZeWrong = findViewsById(R.id.tv_XuanZe_wrong);
        tvTianKongWrong = findViewsById(R.id.tv_TianKong_wrong);
        tvBianChengWrong = findViewsById(R.id.tv_BianCheng_wrong);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initBanner();

        bind();

    }

    private void initBanner() {
            banner.setImageLoader(new GlideImageLoader());
            banner.setImages(list_img);
            banner.start();
    }

    private void initData() {
        right.setText("退出");
        right.setVisibility(View.VISIBLE);
        findViewsById(R.id.top_rl_back).setVisibility(View.GONE);
        banner = (Banner) findViewById(R.id.banner);
        title.setText("首页");
        list_img.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492945103106&di=e0fec23103a2d8bea9615d3d00660c99&imgtype=0&src=http%3A%2F%2Fpx.thea.cn%2FPublic%2FUpload%2F2509129%2FIntro%2F1434014036.jpg");
//        list_img.add("http://pic.qiantucdn.com/58pic/11/79/93/98a58PIC7uf.jpg");
        list_img.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492945152981&di=18f2f0d74bb64d0e28ae7bf9308db15b&imgtype=0&src=http%3A%2F%2Fawb.img1.xmtbang.com%2Fwechatmsg2014%2Farticle201412%2F20141225%2Fd09332b9e8814da2b73660e8b94f95a7.jpg");

    }

    private void bind() {
        tvXuanZe.setOnClickListener(this);
        tvTianKong.setOnClickListener(this);
        tvBianCheng.setOnClickListener(this);
        tvXuanZeWrong.setOnClickListener(this);
        tvTianKongWrong.setOnClickListener(this);
        tvBianChengWrong.setOnClickListener(this);
        findViewsById(R.id.knowledge).setOnClickListener(this);
        findViewsById(R.id.beginExam).setOnClickListener(this);
        findViewsById(R.id.top_rl_right).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_XuanZe://选择
                Intent intent = new Intent(MainActivity.this, XuanZeActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_TianKong://填空
                Intent intent1 = new Intent(MainActivity.this, TianKongActivity.class);
                startActivity(intent1);
                break;
            case R.id.tv_BianCheng://编程题
                Intent intent2 = new Intent(MainActivity.this, BianChengActivity.class);
                startActivity(intent2);
                break;
            case R.id.tv_XuanZe_wrong://选择题目错误
                Intent intent3 = new Intent(MainActivity.this, XuanZeActivity.class);
                intent3.putExtra("type",1);
                startActivity(intent3);
                break;
            case R.id.tv_TianKong_wrong://填空题收藏
                Intent intent4 = new Intent(MainActivity.this, TianKongActivity.class);
                intent4.putExtra("type",1);
                startActivity(intent4);
                break;
            case R.id.tv_BianCheng_wrong://编程题收藏
                Intent intent5 = new Intent(MainActivity.this, BianChengActivity.class);
                intent5.putExtra("type",1);
                startActivity(intent5);
                break;
            case R.id.beginExam://开始考试
                startActivity(ExamActivity.class);
                break;
            case R.id.knowledge://知识点
                startActivity(KnowledgeActivity.class);
                break;
            case R.id.top_rl_right:
                logoutDialog();
                break;
        }
    }

    /**
     * 退出登录提示框
     */
    private void logoutDialog() {
        MyAlertDialog logDialog = new MyAlertDialog(this);
        logDialog.builder()
                .setTitle("退出提示")
                .setMsg("是否要退出登录？")
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                })
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyApplication.getInstance().saveUserInfo(null);
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        startActivity(intent);
                        AppManager.getInstance().finishActivity();
                    }
                })
                .show();
    }

    /**
     * 加载轮播图
     */
    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            /**
             注意：
             1.图片加载器由自己选择，这里不限制，只是提供几种使用方法
             2.返回的图片路径为Object类型，由于不能确定你到底使用的那种图片加载器，
             传输的到的是什么格式，那么这种就使用Object接收和返回，你只需要强转成你传输的类型就行，
             切记不要胡乱强转！
             */
            //Glide 加载图片简单用法
            Glide.with(context).load((String) path).error(R.drawable.default_icon).into(imageView);

        }
    }
}

